//
//  HMPhotoSelectorViewController.swift
//  06-图片选择器
//
//  Created by Admin on 16/9/26.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMPhotoSelectorViewController: UICollectionViewController {
    
    private let maxPhotoOfCount = 6
    
    /// 选择的图片
    var photos = [UIImage]()
    
    // MARK: - 属性
    /// collectionView 的流水布局
    private var layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 构造函数
    init() {
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareCollectionView()
    }
    
    private func prepareCollectionView() {
        // 注册cell
        collectionView?.register(HMPhotoSelectorCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // 设置背景
        collectionView?.backgroundColor = UIColor.white
        
        // 设置itemSize
        layout.itemSize = CGSize(width: 80, height: 80)
        
        // 设置collectionView的内边距
        self.collectionView?.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 1.photos.count < maxPhotoCount 显示 photos.count + 1
        // 2.photos.count = maxPhotoCount 显示 photos.count
        return photos.count < maxPhotoOfCount ? photos.count + 1 : photos.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMPhotoSelectorCell
    
        cell.backgroundColor = UIColor.brown
        cell.delegate = self
        
        // 显示photos里面对应的图片
        if indexPath.item < photos.count {
            cell.image = photos[indexPath.item]
        } else {
            // 需要显示加号按钮
            cell.setupAddButton()
        }
    
        return cell
    }
}

extension HMPhotoSelectorViewController: HMPhotoSelectorCellDelegate {
    func photoSelectorCellDidClickDeleteButton(cell: HMPhotoSelectorCell) {
        // 先获取点击的是哪个位置的删除按钮
        let indexPath = collectionView?.indexPath(for: cell)
        
        photos.remove(at: indexPath!.item)
        
        collectionView?.reloadData()
    }
    
    func photoSelectorCellDidClickAddButton(cell: HMPhotoSelectorCell) {
        let pickerVC = UIImagePickerController()
        
        // 设置代理
        pickerVC.delegate = self
        
        present(pickerVC, animated: true, completion: nil)
    }
}

// - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo

extension HMPhotoSelectorViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        
        let newImage = image.scaleImage()
        // 将选中的图片添加到图片数组
        photos.append(newImage)
        
        // collectionView 刷新数据
        collectionView?.reloadData()
        
        picker.dismiss(animated: true, completion: nil)
    }
    
//    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String: AnyObject]?) {
//        // 获取到图片
//        print("image: \(image)")
//        
//        picker.dismiss(animated: true, completion: nil)
//    }
}
